package com.tuniu.agents.common.service;

import com.tuniu.agents.common.i18n.I18nConstants;
import com.tuniu.agents.message.MessageRender;
import com.tuniu.agents.util.I18nUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.SneakyThrows;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Template rendering service
 */
@Service
public class FreemarkerService implements MessageRender {

    @Autowired
    private Configuration freemarkerConfiguration; // Automatically inject Spring Boot's Freemarker Configuration

    @Autowired
    private freemarker.template.Configuration freemarkerConfig;

    @Autowired
    private MessageSource messageSource;

    @SneakyThrows
    @Override
    public String renderTemplate(String templateName, Map<String, Object> model) {
        // Get the template
        Template template = freemarkerConfiguration.getTemplate(templateName);

        // Render the template and return the result
        StringWriter writer = new StringWriter();
        template.process(model, writer);
        return writer.toString(); // Return the rendered result
    }

    @SneakyThrows
    @Override
    public String renderTemplate(String templateName, Map<String, String> sub, Map<String, Object> model) {
        Locale locale = I18nUtil.getLocale();
        if (MapUtils.isNotEmpty(sub)) {
            String LanguageTag = sub.get(I18nConstants.LANGUAGE_TAG);
            if (StringUtils.isNotBlank(LanguageTag)) {
                locale = I18nUtil.getLocaleWithLanguageTag(LanguageTag);
            }
        }
        model = new HashMap<>(model);
        model.put(I18nConstants.I18N, new I18nSupport(messageSource, locale));

        Template template = freemarkerConfig.getTemplate(templateName);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }

    @Override
    public String renderTemplate(String templateName, Map<String, String> sub, Object model) {
        return renderTemplate(templateName, sub, Map.of(I18nConstants.MODEL_DATA,model));
    }

    // Create an inner class to handle internationalization
    public static class I18nSupport {
        private final MessageSource messageSource;
        private final Locale locale;

        public I18nSupport(MessageSource messageSource, Locale locale) {
            this.messageSource = messageSource;
            this.locale = locale;
        }

        public String getMessage(String code) {
            return messageSource.getMessage(code, null, locale);
        }

        public String getMessage(String code, Object... args) {
            return messageSource.getMessage(code, args, locale);
        }
    }
}
